package com.neusoft.crm.service.impl;

import com.neusoft.crm.mapper.ClientInfoMapper;
import com.neusoft.crm.po.ClientInfo;
import com.neusoft.crm.po.ClientLinkMan;
import com.neusoft.crm.po.Orders;
import com.neusoft.crm.service.IClientInfoService;
import com.neusoft.crm.utils.Slf4j2Logger;
import org.apache.ibatis.exceptions.PersistenceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 客户信息管理接口实现类
 *
 * @author lanan
 */
@Service
public class ClientInfoServiceImpl implements IClientInfoService {
    @Autowired
    ClientInfoMapper clientInfoMapper;

    @Override
    @Cacheable(value = "ClientInfoList", key = "'List'", unless = "#result==null")
    public List<ClientInfo> showClientInfoList(ClientInfo clientInfo) {
        return clientInfoMapper.showClientInfoList(clientInfo);
    }

    @Override
    @CacheEvict(value = "ClientInfoList", allEntries = true)
    public int updateClientInfo(ClientInfo clientInfo) {
        int influenceLineCount = 0;
        try {
            influenceLineCount = clientInfoMapper.updateClientInfo(clientInfo);
        } catch (PersistenceException e) {
            Slf4j2Logger.LOGGER.error("客户信息更新失败，失败原因为：", e);
        }
        return influenceLineCount;
    }

    @Override
    @Cacheable(value = "ClientInfoList", key = "'ClientInfoByCode:'+#clientInfo.clientCode", unless = "#result==null")
    public ClientInfo selectClientInfoByCode(ClientInfo clientInfo) {
        return clientInfoMapper.selectClientInfoByCode(clientInfo);
    }

    @Override
    @Cacheable(value = "ClientInfoList", key = "'Orders:'+#clientInfo.clientCode", unless = "#result==null")
    public List<Orders> selectOrdersByclientCode(ClientInfo clientInfo) {
        return clientInfoMapper.selectOrdersByclientCode(clientInfo);
    }

    @Override
    @Cacheable(value = "ClientInfoList", key = "#clientLinkMan.clientCode", unless = "#result==null")
    public List<ClientLinkMan> selectClientLinkManByclientCode(ClientLinkMan clientLinkMan) {
        return clientInfoMapper.selectClientLinkManByclientCode(clientLinkMan);
    }

    @Override
    @CacheEvict(value = "ClientInfoList", allEntries = true)
    public int insertClientLinkMan(ClientLinkMan clientLinkMan) {
        int influenceLineCount = 0;
        try {
            influenceLineCount = clientInfoMapper.insertClientLinkMan(clientLinkMan);
        } catch (PersistenceException e) {
            Slf4j2Logger.LOGGER.error("客户联系人新建失败，失败原因为：", e);
        }
        return influenceLineCount;
    }

    @Override
    @CacheEvict(value = "ClientInfoList", allEntries = true)
    public int updateClientLinkMan(ClientLinkMan clientLinkMan) {
        int influenceLineCount = 0;
        try {
            influenceLineCount = clientInfoMapper.updateClientLinkMan(clientLinkMan);
        } catch (PersistenceException e) {
            Slf4j2Logger.LOGGER.error("客户联系人更新失败，失败原因为：", e);
        }
        return influenceLineCount;
    }

    @Override
    @Cacheable(value = "ClientInfoList", key = "'LinkMan'+#clientInfo.clientCode", unless = "#result==null")
    public List<ClientLinkMan> selectClientLinkMan(ClientInfo clientInfo) {
        return clientInfoMapper.selectClientLinkMan(clientInfo);
    }

    @Override
    @CacheEvict(value = "ClientInfoList", allEntries = true)
    public int deleteClientLinkMan(ClientLinkMan clientLinkMan) {
        int influenceLineCount = 0;
        try {
            influenceLineCount = clientInfoMapper.deleteClientLinkMan(clientLinkMan);
        } catch (PersistenceException e) {
            Slf4j2Logger.LOGGER.error("客户联系人删除失败，失败原因为：", e);
        }
        return influenceLineCount;
    }

    @Override
    @CacheEvict(value = "ClientInfoList", allEntries = true)
    public int insertClient(ClientInfo clientInfo) {
        int influenceLineCount = 0;
        try {
            influenceLineCount = clientInfoMapper.insertClient(clientInfo);
        } catch (PersistenceException e) {
            Slf4j2Logger.LOGGER.error("客户新建失败，失败原因为：", e);
        }
        return influenceLineCount;
    }

    @Override
    //@Cacheable(value = "ClientInfoList", key = "'clientName'+#clientInfo.clientName+'AreaId:'+#clientInfo.clientAreaId+'LevelId'+#clientInfo.clientLevelId+'state'+#clientInfo.clientState", unless = "#result==null")
    public List<ClientInfo> selectClientInfo(ClientInfo clientInfo) {
        return clientInfoMapper.selectClientInfo(clientInfo);
    }

    @Override
    @CacheEvict(value = "ClientInfoList", allEntries = true)
    public int updateClientStatus(ClientInfo clientInfo) {
        int influenceLineCount = 0;
        try {
            influenceLineCount = clientInfoMapper.updateClientStatus(clientInfo);
        } catch (PersistenceException e) {
            Slf4j2Logger.LOGGER.error("客户状态更新失败，失败原因为：", e);
        }
        return influenceLineCount;
    }

}
